package LFhalf {
	import flash.events.Event;
	import tea.lib.Point3d;
	import tea.lib.BmpList;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	public class Game
	{
		static private var _intance:Game;
		static public function get instance():Game{
			if (_intance == null) {
				_intance = new Game();
			}
			return _intance;
		}
		public function Game() :void{
			
		}
		//var
		private var gameStage:String;
		private var gameTimer:Timer;
		
		public var framePasted:uint;
		public var pastTimer:uint;
		
		private var objectList:Array;
		private var charList:Array;
		private var effectList:Array;
		private var itemList:Array;
		private var ballList:Array;
		private var subCharList:Array;
		private var stageObjectList:Array;
		private var bgLayerList:Array;
		private var fgLayerList:Array;
		
		private var playerList:Array;
		//public
		public function init():void {
			objectList = [];
			charList = [];
			effectList = [];
			itemList = [];
			ballList = [];
			subCharList = [];
			stageObjectList = [];
			bgLayerList = [];
			fgLayerList = [];
			playerList = [];
			framePasted = 0;
		}
		public function dispose():void {
			
		}
		public function tempStart():void {
			gameStage = 'leeonroad';
			addObj('louis', new Point3d(5, 10, 15), { team:0, controler:1 } );
			addObj('template', new Point3d(50, 10, 15), { team:0, controler:2 } );
			initStage(gameStage)
			//set load all data
			BmpList.shareBmpList.addEventListener("load_all",gameStart);
		}
		
		private function initStage(_tag:String):void 
		{
			var stageData:XMLList = new XMLList(DataProcesser.instance.getStage(_tag));
			var tempbmpList:Array = [];
			//background
			for (var bgKey:String in stageData.bg) {
				if (stageData.bg[bgKey] != '') {
					tempbmpList.push(String(stageData.bg[bgKey]));
					var bgObj:Object = { path:String(stageData.bg[bgKey]) };//loop,start,rate
					bgLayerList.push(bgObj);
				}
			}
			//frontground
			for (var fgKey:String in stageData.fg) {
				//trace(stageData.fg[fgKey],fgKey)
			}
			//add photo to cache
			BmpList.shareBmpList.addPhotos(tempbmpList);
		}
		
		public function gameStart(e=null):void 
		{
			trace('game start')
			//
			gameTimer = new Timer(1000 / Main.instance.fps);
			gameTimer.addEventListener(TimerEvent.TIMER, runGame);
			gameTimer.start();
			Render.instance.init(Main.instance.mainStage);
			Main.instance.mainStage.addEventListener(Event.ENTER_FRAME, doRender);
		}
		
		private function doRender(e:Event):void 
		{
			Render.instance.doRender();
		}
		
		private function runGame(e:Event):void 
		{
			//if(!pause)
			framePasted++;
			gameLoop.instance.runLoop();
		}
		public function addObj(id:String, position:Point3d, option:Object = null):void {
			var _obj:phyObject;
			switch(DataProcesser.instance.getType(id)) {
				case 'char':
					_obj = new char(id,option);
					charList.push(_obj);
					
				break;
				default:
				
			}
			_obj.setPosition(position, true);
			objectList.push(_obj);
			//
			//
		}
		//private
		
	}
}